﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.CourseOfLecture;
using PowerCreator.LMS.Web.Mavin;
using PowerCreator.LMS.CourseOfLecture.Entity;

public partial class sysadmin_CourseOfLectureManager_addCourseOfLecture : PowerCreator.LMS.Web.Base.BasePage
{
    CourseOfLecture objCOL = new CourseOfLecture();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            BinderAuthor();
            int COLID = 0; 
            if (Request.QueryString["COLID"] != null)
            {
                COLID = int.Parse(Request.QueryString["COLID"].ToString());
                ViewState["COLID"] = COLID.ToString();
                BinderForm(COLID);
            }

        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle_AddCOL.Text = GetLanageValueByKey("lab_AddCOL_AddCOL", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_COLBaseInfo.Text = GetLanageValueByKey("lab_AddCOL_COLsSet", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_COLTitle.Text = GetLanageValueByKey("lab_AddCOL_COLsTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_COLAuthor.Text = GetLanageValueByKey("lab_AddCOL_COLAuthor", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_COLTime.Text = GetLanageValueByKey("lab_AddCOL_COLTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_COLType.Text = GetLanageValueByKey("lab_AddCOL_COLType", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Zhibo.Text = GetLanageValueByKey("lab_AddCOL_COLType1", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Zhibo.Checked = true;
        radbtn_Dianbo.Text = GetLanageValueByKey("lab_AddCOL_COLType2", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private void BinderAuthor()
    {
        DataTable dt = new DataTable();
        Mavin objMavin = new Mavin();
        dt = objMavin.Query("");
        ddl_COLAuthorID.DataSource = dt;
        ddl_COLAuthorID.DataTextField = "MavinName";
        ddl_COLAuthorID.DataValueField = "MavinID";
        ddl_COLAuthorID.DataBind();
    }
    private void BinderForm(int id)
    {
        VOTCourseOfLecture vo = new VOTCourseOfLecture();
        vo = objCOL.Load(id);
        txt_COLTitle.Text=vo.COLTitle;
        if (vo.FKCOLAuthorID != null)
        {
            if (vo.FKCOLAuthorID.ToString().Length > 0)
            {
                ddl_COLAuthorID.SelectedValue = vo.FKCOLAuthorID.ToString();
            }
        }
        txt_COLTime.Text = vo.COLtime.ToString();
        if (vo.COLType == 0)
        {

            radbtn_Zhibo.Checked = true;
            radbtn_Dianbo.Checked = false;
        }
        else if (vo.COLType == 1)
        {
            radbtn_Zhibo.Checked = false;
            radbtn_Dianbo.Checked = true;
        }
        else
        {
            radbtn_Zhibo.Checked = false;
            radbtn_Dianbo.Checked = false;
        }
    }
    private bool CheckPageData()
    {
        string search = string.Format("COLTitle = '{0}'", txt_COLTitle.Text.Trim());
        if (ViewState["COLID"] != null)
        {
            search += "and COLID<>" + ViewState["COLID"].ToString();
        }
        if (objCOL.Count(search) != 0)
        {
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddCOL_msg", PowerCreator.LMS.Language.LanagetItemType.Page); 
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        lbl_msg_Error.Text = "";
        if (!CheckPageData()) return;
        VOTCourseOfLecture vo = new VOTCourseOfLecture();
        if (ViewState["COLID"] != null)
        {
            vo = objCOL.Load(int.Parse(ViewState["COLID"].ToString()));
        }
        vo.COLTitle = txt_COLTitle.Text.Trim();
        if (txt_COLTime.Text.Trim().Length > 0)
        {
            try
            {
                vo.COLtime = DateTime.Parse(txt_COLTime.Text.Trim());
            }
            catch
            {
                lbl_COLTime_Msg.Text = GetLanageValueByKey("msg_AddCOL_Time_msg", PowerCreator.LMS.Language.LanagetItemType.Page);
                return;
            }
        }
        vo.FKCOLAuthorID = int.Parse(ddl_COLAuthorID.SelectedValue);
        if (radbtn_Zhibo.Checked == true)
            vo.COLType = 0;
        if (radbtn_Dianbo.Checked == true)
            vo.COLType = 1;
        vo.COLCreatetime = DateTime.Now;
        PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
        vo.FKCOLCreatorID = cinfo.UserID;

        if (ViewState["COLID"] == null)
        {
            if (objCOL.Add(vo))
            {
                string url = "CourseOfLectureManager.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            if (objCOL.Update(vo))
            {
                string url = "CourseOfLectureManager.aspx";
                Script.RedirectURL(url);
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "CourseOfLectureManager.aspx";
        Script.RedirectURL(url);
    }
}
